import EventCenter from "../../TFrame/script/helper/EventCenter";
import EventNames from "../EventNames";
import FireBaseAPI from "../FireBaseAPI";
import { ITask } from "../logic/ITask";
import { ActionType } from "../net/Enum";
import ServiceLogic from "../net/ServerLogic";
import CoinSystem from "../system/CoinSystem";
import DailyTaskSystem from "../system/DailyTaskSystem";
import Util from "../../../StartAb/Util";
import WXRealLogUtil from "../../../StartAb/WXRealLogUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class DailyTaskItem extends cc.Component {

    // @property(cc.Button)
    btnGetReward: cc.Button = null;
    @property(cc.Node)
    completedNode: cc.Node = null;

    @property(cc.Label)
    desLabel: cc.Label = null;
    @property(cc.Label)
    rewardCoinCntLabel: cc.Label = null;
    @property(cc.Label)
    progressLabel: cc.Label = null;
    @property(cc.ProgressBar)
    progressBar: cc.ProgressBar = null;
    @property(cc.Label)
    buttonText: cc.Label = null;

    @property(cc.SpriteFrame)
    notDoneSpriteFrame:cc.SpriteFrame = null;

    _taskId = 0;
    _task: ITask = null;
    onLoad() {
        //要注意，如果子节点再加button,就有问题了
        this.btnGetReward = this.node.getComponentInChildren(cc.Button);

        this._taskId = parseInt(this.node.name);
        this._task = DailyTaskSystem.taskList[this._taskId];
        if(this._task == null) {
            this.node.active = false
            return
        }
        let isRewardGot = DailyTaskSystem.dailyInfo.rewardGotTask[this._taskId] == 1;
        this.progressLabel.string = this._task.progressString();
        this.rewardCoinCntLabel.string = this._task.getRewardCoin().toString();
        this.completedNode.active = isRewardGot
        this.btnGetReward.interactable = !isRewardGot;//(!isRewardGot && this._task.isDone());
        this.btnGetReward.node.active = !this.completedNode.active
        this.progressBar.progress = this._task.progressPercent();
        this.desLabel.string = this._task.description()

        var clickEventHandler = new cc.Component.EventHandler();
        clickEventHandler.target = this.node; // 这个 node 节点是你的事件处理代码组件所属的节点
        clickEventHandler.component = "DailyTaskItem";// 这个是代码文件名
        clickEventHandler.handler = "getRewardClick";
        clickEventHandler.customEventData = "";

        this.btnGetReward.clickEvents.push(clickEventHandler);

        this.updateStatusUI();
    }

    updateStatusUI() {
        if(this._task.isDone()) {
            this.buttonText.string = Util.getMulLangText("get")
        } else {
            this.buttonText.string = this._task.getDoText()
            this.btnGetReward.node.getComponent(cc.Sprite).spriteFrame = this.notDoneSpriteFrame
        }
    }

    getRewardClick(e: cc.Event.EventTouch) {
        if(this._task.isDone()) {
            EventCenter.emit(EventNames.FlyGetCoinEvent, this._task.getRewardCoin(), this.node.convertToWorldSpaceAR(cc.Vec2.ZERO));
            CoinSystem.addCoin(this._task.getRewardCoin());
            this.btnGetReward.node.active = false;
            this.completedNode.active = true;
            DailyTaskSystem.requestGetReward(this._taskId);
            EventCenter.emit(EventNames.DailyTaskGetReward)
            WXRealLogUtil.info(`任务${this._task.description()}领奖`)
            // ServiceLogic.action(ActionType.ActionDailyTask, { "beiz": this._taskId })
            // ServiceLogic.action(ActionType.ActionCoinChanged, { "coinhave": CoinSystem.coinInMomory, "beiz": this._task.getRewardCoin() })
        } else {
            Util.disableNodeButton(e.target)
            this._task.toComplete();
        }
    }
}
